package com.sanrenzu.everybodytv.ui.recommend.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.bean.sms.RecBean;
import com.sanrenzu.everybodytv.widget.GlideRoundTransform;

import java.util.List;

/**
 * Created by asus on 2016/9/6.
 */
public class RecommendGirdViewAdapter extends BaseAdapter {

    private List<RecBean> list;
    private Context mContext;
    private LayoutInflater mInflater;
    private RequestManager glideRequest;

    public RecommendGirdViewAdapter(Context mContext, List<RecBean> list) {
        this.mContext = mContext;
        this.list = list;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mHolder = null;
        if(convertView==null) {
            convertView = mInflater.inflate(R.layout.girdview_item_recommend, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        }else{
            mHolder = (ViewHolder) convertView.getTag();
        }
        mHolder.textViewItemNick.setText(list.get(position).getNick());
        mHolder.textViewItemTitle.setText(list.get(position).getTitle());
        glideRequest = Glide.with(mContext);
        glideRequest.load(list.get(position).getAvatar()).transform(new GlideRoundTransform(mContext))
                .into(mHolder.imageViewItemAvatar);
        glideRequest.load(list.get(position).getThumb()).into(mHolder.imageViewItemThumb);
        return convertView;
    }

    static class ViewHolder {
        ImageView imageViewItemAvatar;
        TextView textViewItemNick;
        TextView textViewItemTitle;
        ImageView imageViewItemThumb;

        ViewHolder(View view) {
            imageViewItemAvatar = (ImageView) view.findViewById(R.id.imageView_item_avatar);
            textViewItemNick = (TextView) view.findViewById(R.id.textView_item_nick);
            textViewItemTitle = (TextView) view.findViewById(R.id.textView_item_title);
            imageViewItemThumb = (ImageView) view.findViewById(R.id.imageView_item_thumb);
        }
    }
}
